<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2018/11/8
 * Time: 下午4:21
 */

namespace Api\Controller;


use Api\Service\LogService;
use Mobile\Model\configModel;
use Think\Controller;

class BaseController extends Controller
{
    static public $codeSuccess='100';
    static public $codeError='400';
    public function __construct()
    {
        //dump($_POST['goodsInfo']);die;

        parent::__construct();
        $this->_getConfig();
    }

    //获取数据库中的配置文件
    protected function _getConfig(){
        $configModel = new configModel();
        $where = array(
            "status" => 1,
        );
        $res = $configModel->getConfigList($where);
        if(!empty($res)){
            foreach($res as $k=>$v){
                C($v['cname'],$v['cvalue']);
            }
        }
        return true;
    }

    /**
     * @param $code
     * @param $message
     * @param null $data
     */
    protected function ajax($code,$message,$data=null,$dataName=null){
        if($data!=null && $data!==0){
            if($dataName==null){
                $this->ajaxReturn(array('code'=>$code,'msg'=>$message,'data'=>$data));
                exit();
            }else{
                $this->ajaxReturn(array('code'=>$code,'msg'=>$message,$dataName=>$data));
                exit();
            }
        }else{
            $this->ajaxReturn(array('code'=>$code,'msg'=>$message,'data'=>array())) ;
            exit();
        }
    }

    protected function ajaxException($ex,$data,$isJsonEncode=false){
        if($isJsonEncode==true){
            json_encode($data);
        }
        LogService::S()->writeLog('Exception',$ex->getMessage(),$data);
        $this->ajax(202,'接口异常');
    }

    /**
     *  重组数据
     * @param $must
     * @param $allow
     * @return array
     */
    protected function abData($must,$allow){
        $newData=array();

        //必填数据
        foreach ($must as $k=>$v){
            $newData[$v]=I($v);
            if(empty($newData[$v])){
                $this->ajax(202,$v."不允许为空");
            }
        }

        //选填数据
        foreach ($allow as $k=>$v){
            if($newData[$v]){
                continue;
            }
            $newData[$v]=I($v);
            if(!empty($newData[$v])){
                continue;
            }else{
                unset($newData[$v]);
            }
        }

        return $newData;
    }
}